<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Sclass;
use Input;

class SclassController extends Controller
{
    /**
     * Display a listing of the resource.
     * @author  kowy
     * @time 2017-12-13
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取所有的数据
        $datas = Sclass::all();
        return view('admin.sclass.index',compact('datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.sclass.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //进行验证
        $this -> validate($request,[
            'money'              => 'required|numeric',
            'effecttime'            => 'required|numeric',
        ],[
            'money.required'     => '缺少钱数',
            'money.numeric'      => '钱数应为数字',
            'effecttime.required'   => '缺少有效期',
            'effecttime.numeric'    => '有效期为数字',
        ]);
        $money = Input::get('money');
        $effecttime   = Input::get('effecttime');
        if($effecttime == '1'){
            $rank = $money.'元/年';
        }else{
            $rank = $money.'元/'.$effecttime.'年';
        }
        $res = Sclass::create(['rank'=>$rank,'money'=>$money,'effecttime'=>$effecttime]);
        if($res)
        return back() -> with(['info'=>'添加成功']);
        return back() -> with(['info'=>'添加失败,请重新添加']);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //通过id获取当前的数据
        $datas = Sclass::find($id);
        return view('admin.sclass.edit',compact('datas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //对当前数据进行验证
        $this -> validate($request,[
            'money'              => 'required|numeric',
            'effecttime'            => 'required|numeric',
        ],[
            'money.required'     => '缺少钱数',
            'money.numeric'      => '钱数应为数字',
            'effecttime.required'   => '缺少有效期',
            'effecttime.numeric'    => '有效期为数字',
        ]);
        //获取数据
        $money   = trim(Input::get('money'));
        $effecttime = trim(Input::get('effecttime'));
        if($effecttime == '1'){
            $rank = $money.'元/年';
        }else{
            $rank = $money.'元/'.$effecttime.'年';
        }
        $res = Sclass::find($id)->update(['rank'=>$rank,'money'=>$money,'effecttime'=>$effecttime]);
        if($res)
        return back() -> with(['info'=>'更新成功']);
        return back() -> with(['info'=>'更新失败,请重新提交']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = Sclass::destroy($id);
        if($res)
        return Response() -> json(['status'=>'success','msg'=>'删除成功']);
        return Response() -> json(['status'=>'error','msg'=>'删除失败']);
    }
}
